package com.xlh.openai.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xlh.openai.dao.WechatUserDao;
import com.xlh.openai.entity.WechatUserEntity;
import com.xlh.openai.exception.NoLoginException;
import com.xlh.openai.exception.ServiceException;
import com.xlh.openai.service.WechatUserService;
import com.xlh.openai.vo.UserInfoVO;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author chufangzheng
 */
@Service("wechatUserService")
public class WechatUserServiceImpl extends ServiceImpl<WechatUserDao, WechatUserEntity> implements WechatUserService {

    /**
     * session中openId
     */
    public static final String SESSION_OPENID_KEY = "openid";
    /**
     * session中用户ID
     */
    public static final String SESSION_USERID_KEY = "userid";

    @Override
    public void save(String openId) {

        // 1. 查询当前用户是否存在
        WechatUserEntity wechatUserEntity = getByOpenId(openId);

        if(ObjectUtil.isNotNull(wechatUserEntity)){
            // 更新登陆时间
            UpdateWrapper<WechatUserEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().set(WechatUserEntity::getUpdateTime, new Date())
                         .eq(WechatUserEntity::getId, wechatUserEntity.getId());
            update(updateWrapper);
        }else{
            // 添加新增用户
            WechatUserEntity newUser = new WechatUserEntity();
            newUser.setOpenid(openId);
            save(newUser);
        }
    }

    @Override
    public WechatUserEntity getByOpenId(String openId) {
        return getOne( new QueryWrapper<WechatUserEntity>().lambda().eq(WechatUserEntity::getOpenid, openId));
    }

    @Override
    public void consumeTime(WechatUserEntity wechatUserEntity) {

        // 1. 查询当前用户是否存在
        String openId =  wechatUserEntity.getOpenid();
        if(wechatUserEntity.getQuestionTime() == 0){
            return;
        }
        // 提问次数减一
        Integer questionTime = wechatUserEntity.getQuestionTime();
        UpdateWrapper<WechatUserEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(WechatUserEntity::getUpdateTime, new Date())
                .set(WechatUserEntity::getQuestionTime, (questionTime-1))
                .eq(WechatUserEntity::getOpenid, openId);
        update(updateWrapper);
    }

    @Override
    public String getOpenIdBySession(HttpSession session) {

        if(session.getAttribute(WechatUserServiceImpl.SESSION_OPENID_KEY) == null){
            throw new ServiceException("登陆已过期，请从公众号重新进入！");
        }
        String openId = (String)session.getAttribute(WechatUserServiceImpl.SESSION_OPENID_KEY);
        return openId;
    }

    @Override
    public String addQuestionTime(Integer userId, Integer sku) {

        WechatUserEntity wechatUserEntity = getById(userId);
        UpdateWrapper<WechatUserEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(WechatUserEntity::getUpdateTime, new Date())
                .set(WechatUserEntity::getQuestionTime, (wechatUserEntity.getQuestionTime() + sku))
                .eq(WechatUserEntity::getId, userId);
        update(updateWrapper);
        return wechatUserEntity.getOpenid();
    }

    @Override
    public WechatUserEntity getLoginUserInfo(HttpSession session) {
        String openId = getOpenIdBySession(session);
        return getByOpenId(openId);
    }


}